package com.weixing.mall.provider.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.AgentBonusGroup;
import com.weixing.mall.provider.model.query.AgentBonusGroupQuery;
import com.weixing.mall.provider.model.vo.AgentBonusGroupVo;
import com.weixing.mall.provider.service.IAgentBonusGroupService;
import com.weixing.mall.provider.service.IAgentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 代理分红团队接口
 *
 * @author  wx
 * @date  2019/10/16 14:14
 * @since  1.0
 */
@RestController
@RequestMapping("agent/bonus/group")
public class AgentBonusGroupController extends BaseController {
    @Autowired
    private IAgentBonusGroupService agentBonusGroupService;
    @Autowired
    private IAgentService agentService;

    /**
     * 代理商分红团队列表
     *
     * @param query  查询条件
     * @return       分红团队列表
     */
    @GetMapping("")
    public Result<IPage<AgentBonusGroupVo>> List(AgentBonusGroupQuery query) {
        IPage<AgentBonusGroupVo> list = agentBonusGroupService.selectListOfPage(initMpPage(), query);
        return ResultUtil.success(list);
    }


    /**
     * 代理商分红团队保存/修改
     *
     * @param group        分红团队
     * @return              分红团队
     */
    @PostMapping("")
    public Result<IPage<AgentBonusGroupVo>> save(AgentBonusGroup group) {
//        // 业绩团队
        Long agentId = group.getAgentId();
        if (null == agentId) {
            return ResultUtil.error("业绩团队不能为空");
        }
        if (null == group.getId()) {
            AgentBonusGroup agentBonusGroup = agentBonusGroupService.findByAgentId(agentId);
            if (null != agentBonusGroup) {
                return ResultUtil.error("业绩团队已存在，请选择修改");
            }
        }

//        Long agentId = group.getAgentId();
//        if (null == agentId) {
//            return ResultUtil.error("业绩团队不能为空");
//        }
//        Agent agent = agentService.getById(agentId);
//        if (AgentLevelEnum.LEVEL_1.getValue().intValue() < agent.getAgentLevel().intValue()) {
//            return ResultUtil.error("业绩团队等级必须为一级代理或者经销商");
//        }
//        group.setAgentId(agent.getId());
//        group.setAgentName(agent.getAgentName());
//        group.setAgentLevel(agent.getAgentLevel());
//        // 一级分红团队
//        Long obligeeId1st = group.getObligeeId1st();
//        if (null != obligeeId1st) {
//            Agent obligee1st = agentService.getById(obligeeId1st);
//            if (AgentLevelEnum.LEVEL_1.getValue().intValue() < obligee1st.getAgentLevel().intValue()) {
//                return ResultUtil.error("一级分红团队等级必须为一级代理或者经销商");
//            }
//            group.setObligeeId1st(obligee1st.getId());
//            group.setObligee1st(obligee1st.getAgentName());
//            group.setObligee1stLevel(obligee1st.getAgentLevel());
//            group.setObligee1stCreateTime(new Date());
//            group.setLv1remark("系统后台人工维护一级分红团队信息");
//
//            Long obligor1st = group.getObligor1st();
//            if (null == obligor1st) {
//                return ResultUtil.error("一级分红发放人不能为空");
//            } else {
//                Integer obligorRole1st = group.getObligorRole1st();
//                if (null == obligorRole1st) {
//                    return ResultUtil.error("一级分红发放人角色不能为空");
//                }
//
//            }
//
//        }
//
        if (null == group.getId()) {
            agentBonusGroupService.save(group);
        } else {
            agentBonusGroupService.updateById(group);
        }
        return ResultUtil.success();
    }


}
